﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Runtime.InteropServices;

namespace RemotePrintTimer
{
    public partial class LoginForm : Form
    {
        [DllImport("user32.dll")]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

        const uint SWP_NOSIZE = 0x0001;
        const uint SWP_NOMOVE = 0x0002;
        const int HWND_TOPMOST = -1;
        private static LoginForm instance;
        Action<String> callback = null;
        public LoginForm(Action<String> callback)
        {
            InitializeComponent();
            this.callback = callback;
            NetApi netapi = new NetApi();
            netapi.InitDomainConfig();
            netapi.GetDomainTenant();
            netapi.getUserInfo();
            this.textBox1.Text = GValues.account;
            this.textBox2.Text = GValues.password;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
            {
                MessageBox.Show("用户及密码不能为空！");
                return;
            }
            this.button1.Enabled = false;
            string password = EncryptHelper.Encrypt2(textBox2.Text.Trim(), "1234567890adbcde", "1234567890hjlkew");
            string postData = "{\"username\":\"" + textBox1.Text.Trim() + "\",\"password\":\"" + password + "\",\"captchaFlag\":\"true\"}";
            Http.CreateHttpClient(GValues.domainLink + "sys/login", GValues.tenant, GValues.token).PostAsync(postData, new Action<Http.HttpResp>((r) =>
            {
                var responCode = Convert.ToInt32(r.Code);
                if (responCode == 200) //访问成功
                {
                    ResponResult<LoginDTO> responResult = JsonConvert.DeserializeObject<ResponResult<LoginDTO>>(r.AckStr());
                    if (responResult.success)
                    {
                        GValues.account = textBox1.Text;
                        GValues.password = textBox2.Text; ;
                        GValues.token = responResult.result.token;
                        SqlLiteUtils sqlLite = new SqlLiteUtils();
                        sqlLite.createDb();
                        sqlLite.insertData(GValues.account, StringSecurity.DESEncrypt(textBox2.Text), GValues.tenant, GValues.token);
                        //GValues.globalUrl = GValues.domainLink.Replace("http", "ws").Replace("https", "wss") + "websocket/" + GValues.tenant + "-" + GValues.account + "-print";
                        callback(GValues.account);
                        this.Close();
                        MessageBox.Show("登入成功！");
                    }
                    else
                    {
                        LogHelper.WriteLog("登入系统错误!" + responResult.code + " " + responResult.message, null);
                    }
                }
                else
                {
                    LogHelper.WriteLog("登入系统错误!" + r.Code + " " + " " + responCode + " " + r.Error, null);
                }
            }));
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            SetWindowPos(this.Handle, new IntPtr(HWND_TOPMOST), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
        }
    }
}
